package com.cmict.data.form.api.util;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cmict.data.form.api.entity.dataobject.SysFormDO;
import com.cmict.data.form.api.service.SysCurrencyService;
import com.cmict.data.form.api.service.SysFormService;
import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.infra.api.rdb.RdbApi;
import com.cmict.data.system.api.dept.DeptApi;
import com.cmict.data.system.api.dept.PostApi;
import com.cmict.data.system.api.dept.dto.DeptRespDTO;
import com.cmict.data.system.api.dept.dto.PostDTO;
import com.cmict.data.system.api.dict.DictDataApi;
import com.cmict.data.system.api.dict.dto.DictDataDTO;
import com.cmict.data.system.api.user.AdminUserApi;
import com.cmict.data.system.api.user.dto.AdminUserRespDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.*;

import static com.cmict.data.framework.common.pojo.CommonResult.success;

/**
 * @description: 导入字段类型工具类
 * @author: qiaoyu
 * @create: 2022-07-08 22:57
 **/
@Component
public class InputAssemblyTypeUtil {

    @Autowired
    private SysFormService sysFormService;
    private static SysFormService realsysFormService;

    @Autowired
    private SysCurrencyService sysCurrencyService;
    private static SysCurrencyService realsysCurrencyService;

    @Autowired
    private DictDataApi dictDataService;
    private static DictDataApi realdictDataService;

    @Autowired
    private DeptApi deptService;
    private static DeptApi realdeptService;

    @Autowired
    private AdminUserApi adminUserService;
    private static AdminUserApi realadminUserService;

    @Autowired
    private PostApi postService;
    private static PostApi realpostService;

    @Autowired
    private RdbApi rdbApiService;
    private static RdbApi realrdbapiService;

    @PostConstruct
    public void init() {
        realsysFormService = sysFormService;
        realsysCurrencyService = sysCurrencyService;
        realdictDataService = dictDataService;
        realdeptService = deptService;
        realadminUserService = adminUserService;
        realpostService = postService;
        realrdbapiService = rdbApiService;
    }
    /**
     *映射数据替换通用方法
     */
    @Transactional(rollbackFor = Exception.class)
    public static Map<Object,Object> associatedDataMapping(String tableName, Map<Object,Object> map) {
        SysFormDO sysForm = realsysFormService.selectOneByTableName(tableName);
        JSONObject personData = JSON.parseObject(sysForm.getFormJsonUser());
        JSONArray fields = personData.getJSONArray("fields");
        if(ObjectUtil.isNotEmpty(fields)){
            for (int j = 0; j <fields.size() ; j++) {
                JSONObject field = fields.getJSONObject(j);
                JSONObject config = fields.getJSONObject(j).getJSONObject("__config__");
                String tagIcon = config.getString("tagIcon");
                String datatype = field.getString("dataType");
                String Model = field.getString("__vModel__");
                map = map(field,tagIcon,datatype,Model,map);
            }
        }else{
            JSONObject config = personData.getJSONObject("__config__");
            JSONArray childrenList = config.getJSONArray("children");
            for (int j = 0; j <childrenList.size() ; j++) {
                JSONObject object = (JSONObject) childrenList.get(j);
                JSONObject configchild = object.getJSONObject("__config__");
                JSONArray childfields = configchild.getJSONArray("children");
                for (int k = 0; k <childfields.size() ; k++) {
                    JSONObject children = (JSONObject) childfields.get(k);
                    JSONObject configchildtwo = children.getJSONObject("__config__");
                    String tagIcon = configchildtwo.getString("tagIcon");
                    String datatype = children.getString("dataType");
                    String Model = children.getString("__vModel__");
                    map = map(children,tagIcon,datatype,Model,map);
                }
            }
        }
        return map;
    }

    static Map<Object,Object> map(JSONObject field, String tagIcon, String datatype, String Model, Map<Object,Object> map) {
        //数据字典
        if("dictionary".equals(datatype)){
            String dictType = field.getString("dictName");
            List<DictDataDTO> list = realdictDataService.getListByDictType(dictType).getCheckedData();
            for (int  k= 0; k <list.size() ; k++) {
                if(String.valueOf(list.get(k).getLabel()).equals(map.get(Model))){
                    map.put(Model,list.get(k).getValue());
                }
            }
        }
        //数据源
        if("datasource".equals(datatype)){
            Long disTableName = field.getLong("tableName");
            String disFieldName = field.getString("fieldName");
            String dislinkName = field.getString("saveFieldName");
            if(ObjectUtil.isNotEmpty(disTableName) && ObjectUtil.isNotEmpty(disFieldName)){
                //List<Map<String,Object>> sysDistanceDataVoList = realsysCurrencyService.getDistanceData(disTableName,disFieldName,dislinkName);
                CommonResult json = realrdbapiService.sourceTestList(disTableName);
                Map<Object,Object> mappp = (Map<Object, Object>) json.getData();
                List<Map<Object,Object>>  sysDistanceDataVoList = (List<Map<Object, Object>>) mappp.get("rowMaps");
                if(ObjectUtil.isNotEmpty(sysDistanceDataVoList)){
                    for (int k = 0; k <sysDistanceDataVoList.size() ; k++) {
                        if(String.valueOf(sysDistanceDataVoList.get(k).get(disFieldName)).equals(String.valueOf(map.get(Model)))){
                            map.put(Model,sysDistanceDataVoList.get(k).get(dislinkName));
                        }
                    }
                }
            }
        }
        //部门
        if("depart".equals(tagIcon)){
            List<DeptRespDTO> list = realdeptService.selectListAll().getCheckedData();
            for (int  k= 0; k <list.size() ; k++) {
                if(String.valueOf(list.get(k).getName()).equals(map.get(Model))){
                    map.put(Model,list.get(k).getId());
                }
            }
        }
        //用户
        if("user".equals(tagIcon)){
            List<AdminUserRespDTO> list = realadminUserService.selectListAll();
            for (int  k= 0; k <list.size() ; k++) {
                if(String.valueOf(list.get(k).getUsername()).equals(map.get(Model))){
                    map.put(Model,list.get(k).getId());
                }
            }
        }
        //岗位
        if("post".equals(tagIcon)){
            List<PostDTO> list = realpostService.selectListAll().getCheckedData();
            for (int  k= 0; k <list.size() ; k++) {
                if(String.valueOf(list.get(k).getName()).equals(map.get(Model))){
                    map.put(Model,list.get(k).getId());
                }
            }
        }
        return map;
    }
    /**
     *列表类型
     */
    @Transactional(rollbackFor = Exception.class)
    public static Boolean treeList(String json) {
        JSONObject personData = JSON.parseObject(json);
        JSONObject rightConfig = personData.getJSONObject("rightConfig");
        String panelType = rightConfig.getString("panelType");
        if(panelType.equals("tree-list")){ return false; }
        return true;
    }
}

